package apps;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import reservation.PlageHoraire;
import contraintes.ContrainteDispo;
import fr.irit.smac.may.lib.components.messaging.receiver.AgentRef;

public class BehTB {

	public static AgentRef highestAgent(Map<AgentRef, Integer> in) {

		AgentRef mostCritical = null;
		Integer highestValue = null;

		for (Entry<AgentRef, Integer> e : in.entrySet()) {

			if (mostCritical == null || highestValue == null) {
				mostCritical = e.getKey();
				highestValue = e.getValue();
			} else if (e.getValue().doubleValue() > (highestValue.doubleValue())) {
				mostCritical = e.getKey();
				highestValue = e.getValue();
			}

		}

		return mostCritical;
	}

	public static AgentRef lowestAgent(Map<AgentRef, Integer> in) {

		AgentRef lessCritical = null;
		Integer lowestValue = null;

		for (Entry<AgentRef, Integer> e : in.entrySet()) {

			if (lessCritical == null || lowestValue == null) {
				lessCritical = e.getKey();
				lowestValue = e.getValue();
			} else if (e.getValue().doubleValue() < (lowestValue.doubleValue())) {
				lessCritical = e.getKey();
				lowestValue = e.getValue();
			}

		}

		return lessCritical;
	}

	public static Boolean plageHoraireIsInContrainteDispo(PlageHoraire pH,
			ContrainteDispo cDisp) {
		if ((pH.getDateDeb().after(cDisp.getDateDebut()) && pH.getDateDeb().before(cDisp.getDateFin()))
				|| (pH.getDateFin().after(cDisp.getDateDebut()) && pH.getDateFin().before(cDisp.getDateFin()))
				|| (pH.getDateDeb().before(cDisp.getDateDebut()) && pH.getDateFin().after(cDisp.getDateFin()))
				|| pH.getDateDeb().equals(cDisp.getDateDebut())	|| pH.getDateFin().equals(cDisp.getDateFin())) {
			return true;
		}
		return false;
	}

	public static Boolean plageHoraireIsInContrainteDispoListe(PlageHoraire pH,
			ArrayList<ContrainteDispo> listCDisp) {
		Iterator<ContrainteDispo> iteCDisp = listCDisp.iterator();
		while (iteCDisp.hasNext()) {
			if (plageHoraireIsInContrainteDispo(pH, iteCDisp.next()))
				return true;
		}
		return false;
	}

	public static void retirerDispo(PlageHoraire pH,
			ArrayList<ContrainteDispo> listContrainteDispo) {
		ContrainteDispo temp;
		ArrayList<ContrainteDispo> tempList = new ArrayList<ContrainteDispo>();
		Iterator<ContrainteDispo> iteCDisp = listContrainteDispo.iterator();
		while (iteCDisp.hasNext()) {
			temp = iteCDisp.next();
			if (pH.getDateDeb().before(temp.getDateDebut())
					&& pH.getDateFin().after(temp.getDateFin())) {
			} else {
				if (pH.getDateDeb().after(temp.getDateDebut())
						&& pH.getDateFin().before(temp.getDateFin())) {
					tempList.add(new ContrainteDispo(-1, temp.getDateDebut(),
							pH.getDateDeb()));
					tempList.add(new ContrainteDispo(-1, pH.getDateFin(), temp
							.getDateFin()));
				} else {
					if (pH.getDateDeb().after(temp.getDateDebut())
							&& pH.getDateDeb().before(temp.getDateFin())
							&& pH.getDateFin().after(temp.getDateFin())) {
						tempList.add(new ContrainteDispo(-1, temp
								.getDateDebut(), pH.getDateDeb()));
					} else {
						if (pH.getDateFin().after(temp.getDateDebut())
								&& pH.getDateFin().before(temp.getDateFin())
								&& pH.getDateDeb().after(temp.getDateDebut())) {
							tempList.add(new ContrainteDispo(-1, pH
									.getDateFin(), temp.getDateFin()));
						} else {
							if (pH.getDateDeb().equals(temp.getDateDebut())
									&& pH.getDateFin()
											.equals(temp.getDateFin())) {
							} else {
								tempList.add(temp);
							}
						}
					}
				}
			}
		}
		listContrainteDispo.clear();
		listContrainteDispo.addAll(tempList);
	}
}
